<?php

require_once 'classes/LightPressPlugin.php';

class ThemeSwitcher extends LightPressPlugin {
    
    var $constructor_args = array(
        'allowed_themes' => 'Space-separated list of allowed themes.  Leave blank to allow all available themes.');
    var $default_context = LP_CONTEXT_INDEX;
    var $description = 'Allow site users to switch between available themes';
    
    var $active = true;
    var $hooks = array('start_render', 'sidebar');
    
    var $allowed_themes = '';
    
    function ThemeSwitcher(&$frontend, $args, $dummy_run=false) {
        $this->LightPressPlugin($frontend, $args, $dummy_run);
        
        if (!$dummy_run)
            $this->cookiename = $frontend->options['shortname'] . '-theme';
    }
    
    function setTheme($theme) {
        //get hostname for cookie
        $url = parse_url($this->_frontend->options['url']);
        if (!isset($url['host'])) return;    // stranger things have happened
        $domain = $url['host'];
        if (strtolower(substr($domain, 0, 4)) == 'www.')
            $domain = substr($domain, 4);
        
        setcookie($this->cookiename, trim($theme), time() + 5184000, '/', $domain);
    }
    
    function run($hook, &$payload) {
        $tpl =& $this->_frontend->tpl;
        $options =& $this->_frontend->options;
        $lang_tokens = explode('_', $options['lang']);
        $lang = $lang_tokens[0];
        
        switch($hook) {
            case 'start_render':
                /* check for new theme request var or existing theme cookie */
                if (isset($_REQUEST['lptheme']))
                    $theme = $_REQUEST['lptheme'];
                elseif (isset($_COOKIE[$this->cookiename]))
                    $theme = $_COOKIE[$this->cookiename];
                else
                    return; // do nothing
                
                /* sanitize theme name & check if exists */
                $theme = preg_replace('/[^0-9A-Za-z_\-]/', '', $theme);
                
                $themedir = implode(DIRECTORY_SEPARATOR, array(
                            $options['basedir'], 'themes', $theme, 
                            $lang . '.' . $options['charset']));
                if (!is_dir($themedir))
                    return;     // ignore theme request
                
                /* set theme cookie */
                $this->setTheme($theme);
                
                /* change template directory & related vars*/
                $options['template'] = $theme;
                $tpl->setRoot($themedir);
                $tpl->setVar('option_template', $theme);
                break;
                
            case 'sidebar':
                /* get list of installed themes in current language & charset */
                $themes = array();
                $themebase = implode(DIRECTORY_SEPARATOR, array($options['basedir'], 'themes'));
                if (is_dir($themebase)) {
                    $dir_p = opendir($themebase);
                    while (($name = readdir($dir_p)) !== false) {
                        if ($name[0] == '.')
                            continue;
                        $themedir = implode(DIRECTORY_SEPARATOR, array($themebase, $name,
                                    $lang . '.' . $options['charset']));
                        if (is_dir($themedir))
                            $themes[] = array('theme' => $name, 
                                'selected' => (($name == $options['template']) ? '" selected="' : ''));
                    }
                    closedir($dir_p);
                }
                if (count($themes) == 0) {
                    $this->hide();
                    return;
                }
                
                /* display theme switcher template */
                $tpl->setFile('plugin_themeswitcher', 'plugins/themeswitcher.xml');
                $tpl->parseBlock('BLOCK_THEMESWITCHER', 'themeswitcher', $themes, 'plugin_themeswitcher');
                $tpl->setVar('plugin_themeswitcher_url', $this->_frontend->context['url']);
                $tpl->setVar('plugin_themeswitcher_name', $options['template']);
                $tpl->parse('PLUGIN_THEMESWITCHER', 'plugin_themeswitcher');
        }
    }
    
}
?>